package com.xiaoma.component;

import com.xiaoma.utils.HttpUtils;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xiaoma
 * @Date: 2021/7/14 20:44
 * @description:
 */
@Data
@Component
@ConfigurationProperties(prefix = "alicloud.sms")
public class SmsComponent {

    private String host;

    private String path;

    private String smsSignId;

    private String templateId;

    private String appCode;

    public void sendSmsCode(String phone, String code){

//        String host = "https://gyytz.market.alicloudapi.com";
//        String path = "/sms/smsSend";
        String method = "POST";
//        String appcode = "5ef8458f50494f07a68229d47bf3c0df";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appCode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("param", "**code**:"+code+",**minute**:5");
        querys.put("smsSignId", smsSignId);
        querys.put("templateId", templateId);
        Map<String, String> bodys = new HashMap<String, String>();
        HttpResponse response = null;
        try {
            response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
